// 模块工程化
import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    {
      name: 'default',
      path: '/',
      redirect: { name: 'index' }
    },
    {
      name: 'index',
      path: '/index',
      component: () => import('@/views/index.vue')
    },
    {
      name: 'articleDetail',
      path: '/articleDetail/:id',
      component: () => import('@/views/articleDetail.vue')
    },
    {
      name: 'login',
      path: '/login',
      component: () => import('@/views/login.vue')
    },
    {
      name: 'register',
      path: '/register',
      component: () => import('@/views/register.vue')
    },
    {
      name: 'comments',
      path: '/comments/:id',
      component: () => import('@/views/comments.vue')
    },
    {
      name: 'personal',
      path: '/personal/:id',
      component: () => import('@/views/personal.vue')
    },
    {
      name: 'myfollow',
      path: '/myfollow',
      component: () => import('@/views/myFollows.vue')
    },
    {
      name: 'mystar',
      path: '/mystar',
      component: () => import('@/views/myStars.vue')
    },
    {
      name: 'edit_profile',
      path: '/edit_profile/:id',
      component: () => import('@/views/edit_profile.vue')
    },
    {
      name: 'cateManager',
      path: '/cateManager',
      component: () => import('@/views/cateManager.vue')
    },
    {
      name: 'search',
      path: '/search',
      component: () => import('@/views/search.vue')
    }
  ]
});

router.beforeEach((to, from, next) => {
  // console.log(to);
  if (to.name == 'personal' || to.name == 'edit_profile') { //判断是否跳转到个人中心页
    let token = localStorage.getItem('hm_toutiao');
    if (token) {
      next();
    } else {
      next({ name: 'login' });
    }
  } else {
    next();
  }

});

export default router;